<?php
  require_once(dirname(__FILE__).'/calendrier.lib.php');

/**************************\
 * Classe Rappel          *
\**************************/
class Rappel
{
  var $_db     = NULL;
  var $_date   = '';

  var $dbClass = 'Je_DB_Sql';
  var $dbTable = 'rappel';
  var $contenu = array();

  function Rappel()
  {
    $this->_db = new $this->dbClass;
  }

  // Rappel::SetDate
  function SetDate($date)
  {
    $this->_date = $date;
  }

  // Rappel::SetContenu
  function SetContenu($varname, $value)
  {
    $bRetour = false;
    if ( isset($this->contenu[$varname]) )
    {
      $this->contenu[$varname] = $value;
      $bRetour = true;
    }
    return $bRetour;
  }

  // Rappel::Load
  function Load($id)
  {
    $bLoaded = false;
    $this->_db->query(sprintf( "SELECT * FROM %s WHERE id='%d'"
                             , $this->dbTable
                             , $id ));
    if ($this->_db->next_record())
    {
      $bLoaded = true;
      foreach( $this->contenu as $varname => $value )
        $bLoaded &= $this->SetContenu($varname, $this->_db->f($varname));
    }
    return $bLoaded;
  }

  // Rappel::Create
  function Create($id)
  {
    # Init des valeurs SQL
    $sqlCols   = array_keys($this->contenu);
    $sqlValues = array();
    foreach ($this->contenu as $value)
    {
      $sqlValues[] = sprintf("'%s'", mysql_escape_string($value));
    }

    $this->_db->query(sprintf( "INSERT INTO %s(id,%s) VALUES('%d',%s)"
                             , $this->dbTable
                             , implode(',', $sqlCols)
                             , $id
                             , implode(',', $sqlValues) ));
    return ($this->_db->affected_rows() == 1);
  }

  // Rappel::Update
  function Update($id)
  {
    # Init des valeurs SQL
    $sqlValues = array();
    foreach ($this->contenu as $varname => $value)
    {
      $sqlValues[] = sprintf("%s='%s'" , $varname , $value);
    }

    # Mise a jour
    $this->_db->query(sprintf( "UPDATE %s SET %s WHERE id='%d'"
                             , $this->dbTable
                             , implode(',', $sqlValues)
                             , $id ));
    return ($this->_db->affected_rows() == 1);
  }
}

/**************************\
 * Classe Agenda          *
\**************************/
class Agenda
{
  var $classname        = 'Agenda';
  var $persistent_slots = array( '_dateCurrent'
                               , '_dateDisplay'
                               , '_type' );
  var $dbTable          = 'agenda';
  var $calendrierName   = 'Calendrier';

  var $_dateCurrent = NULL;
  var $_dateDisplay = NULL;
  var $_type = 'm';

  // Agenda::Agenda
  function Agenda($dateCurrent='', $dateDisplay='')
  {
    $this->_dateCurrent = new DateSql($dateCurrent);
    $this->_dateDisplay = new DateSql($dateDisplay);
  }

  // Agenda::SetDateCurrent
  function SetDateCurrent($dateSql)
  {
    $this->_dateCurrent = new DateSql($dateSql);
    return $this->_dateCurrent->IsValide();
  }

  // Agenda::GetDateCurrent
  function GetDateCurrent()
  {
    return $this->_dateCurrent->GetSql();
  }

  // Agenda::SetDateDisplay
  function SetDateDisplay($dateSql)
  {
    $this->_dateDisplay = new DateSql($dateSql);
    return $this->_dateDisplay->IsValide();
  }

  // Agenda::GetDateDisplay
  function GetDateDisplay()
  {
    return $this->_dateDisplay->GetSql();
  }

  // Agenda::SetTypePeriode
  function SetTypePeriode($type)
  {
    $bRetour = false;
    switch ($type)
    {
    case 'm':
    case 't':
    case 'a':
      $this->_type = $type;
      $bRetour = true;
      break;
    }
    return $bRetour;
  }

  // Agenda::GetTypePeriode
  function GetTypePeriode()
  {
    return $this->_type;
  }

  // Agenda::GetRappel
  // TODO : utiliser les tables jointes
  function GetRappel($id)
  {
    $infos = false;
    $reqRappel = sprintf( "SELECT * FROM %s WHERE id='%d'"
                        , $this->dbTable
                        , $id );
    $dbRappel = new Je_DB_Sql($reqRappel);
    if ($dbRappel->next_record())
    {
      # Selection du message OK
      $type = $dbRappel->f('type');
      $rappel = new $type;
      $rappel->SetDate($dbRappel->f('date'));
      if ( $rappel->Load($dbRappel->f('id')) )
      {
        # Recuperation des infos
        $infos = array( 'id'      => $dbRappel->f('id')
                      , 'type'    => $type
                      , 'date'    => $dbRappel->f('date')
                      , 'contenu' => $rappel->contenu );
      }
    }
    return $infos;
  }

  // Agenda::GetRappels
  // TODO : utiliser les tables jointes
  function GetRappels()
  {
    $tabRappels = array();

    $infosDate = $this->_dateDisplay->GetInfos();

    if ( is_array($infosDate) )
    {
      switch ($this->_type)
      {
      case 'm':
        $dateInitiale = sprintf( '%4d-%02d-01'
                               , $infosDate['annee']
                               , $infosDate['mois'] );
        $dateFinale   = sprintf( '%4d-%02d-%02d'
                               , $infosDate['annee']
                               , $infosDate['mois']
                               , $infosDate['nbJours'] );
        break;

      case 't':
        $dateMoisPrecedent = new DateSql( $this->_date->GetMoisPrecedent() );
        $infosDateMoisPrec = $dateMoisPrecedent->GetInfos();
        $dateInitiale = sprintf( '%4d-%02d-01'
                               , $infosDateMoisPrec['annee']
                               , $infosDateMoisPrec['mois'] );

        $dateMoisSuivant = new DateSql( $this->_date->GetMoisSuivant() );
        $infosDateMoisSuiv = $dateMoisSuivant->GetInfos();
        $dateFinale   = sprintf( '%4d-%02d-%02d'
                               , $infosDateMoisSuiv['annee']
                               , $infosDateMoisSuiv['mois']
                               , $infosDateMoisSuiv['nbJours'] );
        break;

      case 'a':
        $dateInitiale = sprintf( '%4d-01-01'
                               , $infosDate['annee'] );
        $dateFinale   = sprintf( '%4d-12-31'
                               , $infosDate['annee'] );
        break;
      }

      $reqRappels = sprintf( "SELECT * FROM %s WHERE date>='%s' AND date<='%s'"
                            ." ORDER BY date ASC"
                           , $this->dbTable
                           , $dateInitiale
                           , $dateFinale );
      $dbRappels = new Je_DB_Sql($reqRappels);

      while ( $dbRappels->next_record() )
      {
        $tabRappels[] = $dbRappels->Record;
      }
    }

    return $tabRappels;
  }

  // Agenda::CreateRappel
  // TODO : utiliser les tables jointes
  function CreateRappel($date, $type, $data)
  {
    $reqInsert = sprintf( "INSERT INTO %s(date, type) VALUES('%s','%s')"
                        , $this->dbTable
                        , $date
                        , $type );
    $dbInsert = new Je_DB_Sql($reqInsert);
    $id = $dbInsert->InsertId();

    $rappel = new $type;
    foreach ($data as $varname => $value)
    {
      $rappel->SetContenu($varname, $value);
    }
    if (!$rappel->Create($id))
      $id = false;

    return $id;
  }

  // Agenda::UpdateRappel
  function UpdateRappel($id, $date, $type, $data)
  {
    $reqUpdate = sprintf( "UPDATE %s SET date='%s' WHERE id='%s'"
                        , $this->dbTable
                        , $id
                        , $date );
    $req = new Je_DB_Sql($reqUpdate);

    $rappel = new $type;
    foreach ($data as $varname => $value)
    {
      $rappel->SetContenu($varname, $value);
    }
    return $rappel->Update($id);
  }

  // Agenda::Get
  function Get()
  {
    $cal = new $this->calendrierName($this->GetDateCurrent(), $this->GetDateDisplay());

    // Replissage du calendrier
    $tabRappels = $this->GetRappels();
    foreach( $tabRappels as $infosRappel )
    {
      $cal->SetInfos($infosRappel['date'], $infosRappel);
    }
    $cal->SetType($this->_type);
    return $cal->Get();
  }
}
?>
